import time
from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse
from web.forms.project import ProjectModelForm
from web import models
from utils.tencent.cos import create_bucket


def project_list(request):
    """项目列表"""
    # print(request.tracer.user)
    # print(request.tracer.price_policy)
    if request.method == "GET":
        project_dic = {'star': [], 'my': [], 'join': []}
        my_project_list = models.Project.objects.filter(creator=request.tracer.user)
        for project in my_project_list:
            if project.star:
                project_dic['star'].append({"value": project, "type": "my"})
            else:
                project_dic['my'].append(project)
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)
        for join_project in join_project_list:
            if join_project.star:
                project_dic['star'].append({"value": join_project.project, "type": "join"})
            else:
                project_dic['join'].append(join_project.project)
        form = ProjectModelForm(request)
        return render(request, 'project.html', {'form': form, 'project_dic': project_dic})
    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 为项目创建一个桶
        bucket = "{}-{}-1302866811".format(request.tracer.user.mobile_phone, str(int(time.time() * 1000)))
        region = "ap-nanjing"
        create_bucket(bucket, region)
        # 创建项目
        form.instance.bucket = bucket
        form.instance.region = region
        form.instance.creator = request.tracer.user
        instance = form.save()
        issues_type_object_list = []
        for item in models.IssuesType.PROJECT_INIT_LIST:  # ["任务", "功能", "BUG"]
            issues_type_object_list.append(models.IssuesType(project=instance, title=item))
        models.IssuesType.objects.bulk_create(issues_type_object_list)
        return JsonResponse({'status': True})
    # 失败返回错误信息
    return JsonResponse({'status': False, 'error': form.errors})


def project_star(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect("websass:project_list")
    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect("websass:project_list")
    return HttpResponse("请求错误")


def unproject_star(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect("websass:project_list")
    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect("websass:project_list")
    return HttpResponse("请求错误")
